<?php
namespace app\activity\model;
use app\BaseModel;
use app\card\model\Company;
use think\facade\Db;



class AtvCompany extends BaseModel
{
    //定义表名
    protected $name = 'lb_activity_activity_company';


    /**
     * @param $dis
     * @param $data
     * @return int
     * 编辑
     */
    public function companyGoodsAdd($data){

        $res = $this->insert($data);
        return $res;
    }
    /**
     * @param $dis
     * @param $data
     * @return int
     * 查询
     */
    public function companyGoodsSelect($dis){
        $data = $this->where($dis)->select()->toArray();
        return $data;
    }

    /**
     * @param $dis
     * @功能说明:获取某个公司绑定的商品id
     * @author lxj
     * @DataTime: 2020/5/14 9:51
     */
    public function getCompanyGoodsIds($dis){
        $ids = $this->where($dis)->column('goods_id');
        return $ids;
    }

    /**
     * @param $dis
     * @param $data
     * @return int
     * 删除
     */
    public function companyGoodsDel($dis,$ids){
        $res = $this->where($dis)->where('id','not in',$ids)->delete();
        return $res;
    }

    /**
     * @param $dis
     * @param $data
     * @return int
     * 查询
     */
    public function companyGoodsFind($dis){
        $data = $this->where($dis)->find();
        return $data;
    }


    /**
     * @param $dis
     * @return array
     * 获取company
     */

    public function getId($dis){
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_company b','a.company_id=b.id')
            ->where($dis)
            ->column('company_id');
        $company_id = [];
        if(!empty($data)){
            foreach ($data as $v){
                $company_id[] = $this->reset($v);
            }
        }
        return array_values($company_id);
    }

    /***
     * @param $dis
     * @功能说明:公共商品获取公司id
     * @author lxj
     * @DataTime: 2020/5/14 14:06
     */
    public function getCompanyIds($dis){

        $company_model = new Company();

        $data = $company_model->where($dis)->column('id');
        $company_id = [];
        if(!empty($data)){
            foreach ($data as $v){
                $company_id[] = $this->reset($v);
            }
        }
        return array_values($company_id);
    }

    /**
     * User: chenniang
     * Date: 2019-09-24 10:51
     * @param $dis
     * @return int
     * descption:获取公司个数
     */
    public function getCount($dis){
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_company b','a.company_id=b.id')
            ->where($dis)
            ->count();
        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-09-25 15:29
     * @return void
     * descption:转换插入数据
     */
    public function changeData($ids){
        $data = [];
        if(is_array($ids)){

            foreach ($ids as $k =>$v){
                $data[] = array_pop($v);
            }
        }

        return $data;
    }

    /**
     * @param $id
     * 回显
     */
    public function reset($id,&$data=array()){
        $cate   = Db::name('longbing_card_company')->where(['id'=>$id])->find();
        if(!empty($cate)&&$cate['pid']!=$cate['id']){
            array_unshift($data,$cate['id']);
            $this->reset($cate['pid'],$data);
        }
        return $data;
    }





}